package com.jxl.coreplay.managers
{
	import com.jxl.coreplay.constants.VideoConstants;
	import com.jxl.coreplay.events.VideoEvent;
	import com.jxl.coreplay.vo.StreamConfigurationVO;
	
	public class LiveStreamManager extends AbstractStreamManager
	{
		public function LiveStreamManager(videoType:StreamConfigurationVO):void
		{
			super(videoType);
			
			if(videoType.type != VideoConstants.LIVE)
			{
				throw new Error("LiveStreamManager can only handle live video.");
				return;
			}
			
			initLiveStream();
		}
		
		protected function initLiveStream():void
		{
			if(corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					createNetStream(corePlayNetConnection.netConnection);
					_videoType.video.attachNetStream(corePlayNetStream);
					if(_videoType.autoPlay == true)
					{
						playLiveStream();
					}
				}
				else
				{
					connect();
				}
			}
			else
			{
				connect();
			}
		}
		
		protected override function onConnectSuccess(event:VideoEvent):void
		{
			initLiveStream();
		}
		
		protected function playLiveStream():void
		{
			if(corePlayNetStream != null && corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					corePlayNetStream.play(_videoType.streamName, -1);
				}
			}
		}
		
		protected function pauseLiveStream():void
		{
			if(corePlayNetStream != null && corePlayNetConnection != null)
			{
				if(corePlayNetConnection.connected == true)
				{
					corePlayNetStream.pause();
				}
			}
		}
		
		protected function stopLiveStream():void
		{
			_videoType.video.attachNetStream(null);
			destroyNetStream();
		}
		
		public override function play():void
		{
			playLiveStream();
		}
		
		public override function pause():void
		{
			pauseLiveStream();
		}
		
		public override function stop():void
		{
			stopLiveStream();
		}
		
		public override function seek(offset:Number):void
		{
			throw new Error("You cannot seek in a Live video stream.");
		}
		
	}
}